package RentCarProject;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class LoginUI extends JFrame {
    JFrame frame;
    JTextField usertext;
    JPasswordField passtext;
    JLabel userlabel,passwordlabel,bglabel;
    MyButton loginbutton,registerbuttton;
    JButton managerbutton,forgetbutton;

    public LoginUI(){
        frame=this;
        setTitle("用户登录");
        setSize(750,450);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        usertext=new JTextField(20);
        usertext.setBounds(330,110,180,20);
        passtext=new JPasswordField(20);
        passtext.setBounds(330,160,180,20);
        userlabel=new JLabel("用户名：");
        userlabel.setFont(new Font("宋体",Font.BOLD,15));
        userlabel.setForeground(Color.white);
        userlabel.setBounds(250,110,70,20);
        passwordlabel=new JLabel("密  码：");
        passwordlabel.setForeground(Color.white);
        passwordlabel.setFont(new Font("宋体",Font.BOLD,15));
        passwordlabel.setBounds(250,160,70,20);
        bglabel=new JLabel(new ImageIcon("src/picture/login.png"));
        bglabel.setOpaque(false);
        bglabel.setBounds(0,0,750,450);

        loginbutton=new MyButton("登录");
        loginbutton.setBounds(270,240,80,40);
        registerbuttton=new MyButton("注册");
        registerbuttton.setBounds(400,240,80,40);
        forgetbutton=new JButton("忘记密码？");
        forgetbutton.setBounds(325,290,100,40);
        forgetbutton.setForeground(Color.CYAN);
        forgetbutton.setContentAreaFilled(false);
        forgetbutton.setBorder(null);
        managerbutton=new JButton("管理员登录");
        managerbutton.setFont(new Font("宋体",Font.BOLD,15));
        managerbutton.setForeground(Color.CYAN);
        managerbutton.setContentAreaFilled(false);
        managerbutton.setBorder(null);
        managerbutton.setBounds(630,380,100,40);

        loginbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserBase userBase=new UserBase();
                userBase.setUsername(usertext.getText());
                userBase.setPassword(passtext.getText());
                try {
                    if(userBase.loGin()){
                        System.out.println("yes");
                        RentCarUI rentCarUI=new RentCarUI();
                        rentCarUI.setUserbase(userBase);
                        rentCarUI.init();
                        dispose();
                    }else{
                        JOptionPane.showMessageDialog(frame,"登录失败！");
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        registerbuttton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegisterUI();
                dispose();
            }
        });
        managerbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new ManagerLoginUI();
            }
        });
        forgetbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ForgetPasswordFrame();
                dispose();
            }
        });
        add(userlabel);
        add(usertext);
        add(passwordlabel);
        add(passtext);
        add(loginbutton);
        add(registerbuttton);
        add(forgetbutton);
        add(managerbutton);
        add(bglabel);
        setVisible(true);
    }

}
